package com.bookseek.xreader.service.dk;

import com.bookseek.xreader.consts.XreaderConts;
import com.bookseek.xreader.dao.THisPoDao;
import com.bookseek.xreader.dbhelper.DbSessionUtils;
import com.bookseek.xreader.po.THisPo;
import com.bookseek.xreader.utils.DLog;
import com.bookseek.xreader.vo.mock.TBookChapterVo;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class THisService {
    private static THisService instance;

    private static THisPoDao tHisPoDao;

    private THisService(){}

    public static THisService getInstance(){
        if (instance == null){
            synchronized (THisService.class){
                if(instance == null){
                    instance = new THisService();
                    tHisPoDao = DbSessionUtils.genDaoSession().getTHisPoDao();
                }
            }
        }
        return instance;
    }

    public THisPo getByCidAndPtype(Long cid,Integer ptype){
        QueryBuilder<THisPo> builder =  tHisPoDao.queryBuilder();
        THisPo po  = builder.where(
                THisPoDao.Properties.Cid.eq(cid),
                THisPoDao.Properties.Ptype.eq(ptype)
        )
                .limit(1)
                .list()
                .stream()
                .findFirst()
                .orElse(null);
        return po;
    }

    public THisPo getByBidAndCidPtype(Long bid,Long cid,Integer ptype ){
        QueryBuilder<THisPo> builder =  tHisPoDao.queryBuilder();
        List<THisPo> pos = builder.where(
                THisPoDao.Properties.Bid.eq(bid),
                THisPoDao.Properties.Cid.eq(cid),
                THisPoDao.Properties.Ptype.eq(ptype)
        ).list();
        THisPo po = pos.stream().findFirst().orElse(null);
        return po;
    }

    /**
     * 根据bid获取最后一次阅读位置，不存在返回空
     * @param bid
     * @return
     */
    public THisPo getBookLastReadInfo(Long bid){
        return getByBidAndPtype(bid,XreaderConts.TMETA_BTYPE_DIANJI).stream().findFirst().orElse(null);
    }


    public List<THisPo> getByBidAndPtype(Long bid, Integer ptype){
        QueryBuilder<THisPo> builder =  tHisPoDao.queryBuilder();
        List<THisPo> pos = builder.where(
                THisPoDao.Properties.Bid.eq(bid),
                THisPoDao.Properties.Ptype.eq(ptype)
        ).orderDesc(THisPoDao.Properties.Tm)
        .list();
        return pos;
    }



    /**
     * 保存典籍阅读历史记录
     * @param po
     */
    public void saveDianJiHis(THisPo po){
        Long bid = po.bid;
        Long cid = po.cid;
        Integer pstart = po.pstart;
        Integer pend = po.pend;
        Integer pindex = po.pindex;
        DLog.info("bid=",bid,"cid=",cid,"pindex=",pindex,"pstart=",pstart,"pend=",pend);
        if(bid == null || cid == null || pstart == null || pend == null || pindex == null){
            DLog.error("缺失必要参数 bid,cid,pstart,pend,pindex");
            return;
        }
        //典籍类型
        po.ptype = XreaderConts.TMETA_BTYPE_DIANJI;
        if(po.tm == null){
            po.tm = System.currentTimeMillis()/1000;
        }
        THisPo dbPo = getByCidAndPtype(cid,po.ptype);
        if(dbPo == null){
            //插入
           long insertId = tHisPoDao.insert(po);
           DLog.info("插入主键=======》insertId",insertId);
        }else{
            //更新
            po.id = dbPo.id;
            tHisPoDao.update(po);
        }
    }


}
